package com.zong.ai.agent.advisor;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.ai.chat.model.MessageAggregator;
import reactor.core.publisher.Flux;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2025/6/17
 */
@Slf4j
public class MyLoggerAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {


    @NotNull
    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        // 打印日志
        log.info("ai request: {}", advisedRequest.userText());
        // 调用下一个advisor
        AdvisedResponse advisedResponse = chain.nextAroundCall(advisedRequest);
        log.info("ai response: {}", advisedResponse.response().getResult().getOutput().getText());
        return advisedResponse;
    }

    @NotNull
    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        log.info("ai request: {}", advisedRequest.userText());

        Flux<AdvisedResponse> advisedResponses = chain.nextAroundStream(advisedRequest);

        return new MessageAggregator().aggregateAdvisedResponse(advisedResponses,
                advisedResponse -> log.debug("ai response: {}", advisedResponse.response().getResult().getOutput().getText()));
    }

    @NotNull
    @Override
    public String getName() {
        return this.getClass().getName();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
